package net.gilgame.bukkit.commands;

import java.util.List;
import java.util.logging.Level;
import net.gilgame.bukkit.CommandHandler;
import net.gilgame.bukkit.EntityAdmin;
import net.gilgame.bukkit.lib.Groups;
import net.gilgame.bukkit.lib.Messages;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

/**
 *
 * @author Gilgame
 * @date 27 November 2011
 */
public class ClearEntity implements ICommand
{
    public boolean run(CommandSender sender, String[] args)
    {
        if (args.length > 2 || args.length < 1)
        {
            sender.sendMessage(Messages.InvalidArguments);
            return false;
        }

        World world = getWorld(sender, args);

        if (world == null)
        {
            sender.sendMessage(Messages.WorldNotFound);
            return true;
        }

        int cleared = 0;
        String name = args[0].toLowerCase();


        if (!CommandHandler.hasPermission(sender, name))
        {
            sender.sendMessage(Messages.NoEAccess);
            return true;
        }

        try
        {
            cleared = clear(world.getEntities(), name);
        }
        catch (Exception e)
        {
            EntityAdmin.LOGGER.log(Level.SEVERE, e.toString());
            sender.sendMessage(Messages.EncounteredError);
            return true;
        }

        String message = String.format(Messages.RemovedEntities, cleared, name);
        sender.sendMessage(message);

        return true;
    }

    private int clear(List<Entity> entities, String name)
    {
        int cleared = 0;

        if (name.equals("animals"))
        {
            for (Entity entity : entities)
            {
                for (String animal : Groups.Animals)
                {
                    if (entity.getClass().getName().toLowerCase().contains(animal))
                    {
                        entity.remove();
                        cleared++;
                    }
                }
            }
        }
        else if (name.equals("monsters"))
        {
            for (Entity entity : entities)
            {
                for (String monster : Groups.Monsters)
                {
                    if (entity.getClass().getName().toLowerCase().contains(monster))
                    {
                        entity.remove();
                        cleared++;
                    }
                }
            }
        }
        else if (name.equals("denizens"))
        {
            for (Entity entity : entities)
            {
                for (String denizen : Groups.Denizens)
                {
                    if (entity.getClass().getName().toLowerCase().contains(denizen))
                    {
                        entity.remove();
                        cleared++;
                    }
                }
            }
        }
        else if (name.equals("projectiles"))
        {
            for (Entity entity : entities)
            {
                for (String projectile : Groups.Projectiles)
                {
                    if (entity.getClass().getName().toLowerCase().contains(projectile))
                    {
                        entity.remove();
                        cleared++;
                    }
                }
            }
        }
        else if (name.equals("environment"))
        {
            for (Entity entity : entities)
            {
                for (String environment : Groups.Environment)
                {
                    if (entity.getClass().getName().toLowerCase().contains(environment))
                    {
                        entity.remove();
                        cleared++;
                    }
                }
            }
        }
        else
        {
            for (Entity entity : entities)
            {
                if (entity.getClass().getName().toLowerCase().contains(name))
                {
                    entity.remove();
                    cleared++;
                }
            }
        }

        return cleared;
    }

    private World getWorld(CommandSender sender, String[] args)
    {
        if (args.length == 2)
        {
            return sender.getServer().getWorld(args[1]);
        }
        else
        {
            if (sender instanceof Player)
            {
                return ((Player)sender).getWorld();
            }
            else
            {
                return null;
            }
        }
    }
}
